<template>
	<view>
		<view class="container">
			<view class="form">
				<view class="formitem">
					<view class="label">
						姓名
					</view>
					<input class="input" v-model="form.name" placeholder="请输入真实姓名" placeholder-class="placeholder"
						type="text">
					<image class="more_icon" src="/static/img/more.png" mode=""></image>
				</view>
				<view class="formitem">
					<view class="label">
						代理地址
					</view>
					<input class="input" v-model='form.address' placeholder="请输入代理区、县详细地址"
						placeholder-class="placeholder" type="text">
					<image class="more_icon" src="/static/img/more.png" mode=""></image>
				</view>
				<view class="formitem">
					<view class="label">
						联系电话
					</view>
					<input class="input" v-model="form.phone" placeholder="请输入联系电话" placeholder-class="placeholder"
						type="text">
					<image class="more_icon" src="/static/img/more.png" mode=""></image>
				</view>
				<view class="formitem">
					<view class="label">
						身份证号码
					</view>
					<input class="input" v-model="form.idcard" placeholder="请输入身份证号码" placeholder-class="placeholder"
						type="text">
					<image class="more_icon" src="/static/img/more.png" mode=""></image>
				</view>
				<view class="form_title">
					<div class="line"></div>
					<text style="color: #333333;font-size: 30rpx;font-weight: bold;">企业营业执照</text>
					<text style="color: #8B8B8B;font-size: 24rpx;">(请上传清晰无水印图片）</text>
				</view>
				<view class="pic_box">
					<view class="pic_item" v-for="(item,index) in fileList6" :key="index">
						<image class="pic_bg" @click="imgPreview(fileList6,index)" :src="baseurl+item"
							mode="aspectFill"></image>
						<view class="pic_item_name" style="background-color: rgba(0, 0, 0, 0.5);"
							@click="resup(index,'fileList6')">
							重新上传
						</view>
						<view class="delbox" @click="del(index,'fileList6')">
							<image class="del_icon" src="./static/picdel.png" mode=""></image>
						</view>
					</view>
					<view class="pic_item" @click="up('fileList6')" v-if="!fileList6.length>0">
						<image class="pic_bg" src="./static/up.png" mode=""></image>
					</view>
				</view>
				<view class="form_title">
					<div class="line"></div>
					<text style="color: #333333;font-size: 30rpx;font-weight: bold;">食品经营许可证</text>
					<text style="color: #8B8B8B;font-size: 24rpx;">(请上传清晰无水印图片）</text>
				</view>
				<view class="pic_box">
					<view class="pic_item" v-for="(item,index) in fileList7" :key="index">
						<image class="pic_bg" @click="imgPreview(fileList7,index)" :src="baseurl+item"
							mode="aspectFill"></image>
						<view class="pic_item_name" style="background-color: rgba(0, 0, 0, 0.5);"
							@click="resup(index,'fileList7')">
							重新上传
						</view>
						<view class="delbox" @click="del(index,'fileList7')">
							<image class="del_icon" src="./static/picdel.png" mode=""></image>
						</view>
					</view>
					<view class="pic_item" @click="up('fileList7')" v-if="!fileList7.length>0">
						<image class="pic_bg" src="./static/up.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view class="btn" @click="submit">
			确认提交
		</view>
	</view>
</template>

<script>
	import {
		addSubmit
	} from '@/api/my.js'
	export default {
		data() {
			return {
				pt: 0,
				baseurl: '',
				fileList6: [],
				fileList7: [],
				form: {
					name: '',
					address: '',
					phone: '',
					buisness_image: '',
					food_image: '',
					idcard: ''
				},
				type: ''
			};
		},
		onLoad(option) {
			this.type = option.type
			this.baseurl = this.$BASE_URL
			this.pt = uni.getStorageSync('statusBarHeight')
		},
		methods: {
			imgPreview(urls, i) {
				console.log(urls)
				let us = []
				urls.map(t => {
					us.push(this.baseurl + t)
				})
				uni.previewImage({
					urls: us,
					current: i
				});
			},
			// 新增图片
			async up(type) {
				let _this = this
				await uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						tempFilePaths.map(t => {
							uni.showLoading({
								title: '上传中'
							})
							uni.uploadFile({
								url: _this.$BASE_URL +
									'/api/common/upload', //仅为示例，非真实的接口地址
								filePath: t,
								name: 'file',
								header: {
									token: uni.getStorageSync('token')
								},
								success: (uploadFileRes) => {
									this[type].push(JSON.parse(uploadFileRes.data)
										.data.url)
									uni.hideLoading()
									uni.showToast({
										title: JSON.parse(uploadFileRes.data)
											.msg,
										icon: 'none'
									})
								},

							});
						})
					}
				});
			},
			del(i, type) {
				this[type].splice(i, 1)
			},
			async resup(i, type) {
				let _this = this
				await uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						uni.showLoading({
							title: '上传中'
						})
						uni.uploadFile({
							url: _this.$BASE_URL +
								'/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								token: uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								uni.hideLoading()
								_this.$set(_this[type], i, JSON.parse(uploadFileRes.data)
									.data
									.url)
								uni.showToast({
									title: JSON.parse(uploadFileRes.data).msg,
									icon: 'none'
								})
							},
						});
					}
				});
			},
			submit() {
				this.form.buisness_image = this.fileList6[0]
				this.form.food_image = this.fileList7[0]
				let {
					name,
					address,
					phone,
					buisness_image,
					food_image,
					idcard
				} = this.form
				if (!name) {
					uni.showToast({
						title: '请输入真实姓名',
						icon: 'none'
					})
					return
				}
				if (!address) {
					uni.showToast({
						title: '请输入代理地址',
						icon: 'none'
					})
					return
				}
				if (!phone) {
					uni.showToast({
						title: '请输入联系人电话',
						icon: 'none'
					})
					return
				}
				if (!phone) {
					uni.showToast({
						title: '请输入联系电话',
						icon: 'none'
					})
					return
				}
				if (!idcard) {
					uni.showToast({
						title: '请输入身份证号码',
						icon: 'none'
					})
					return
				}
				if (!buisness_image) {
					uni.showToast({
						title: '请上传营业执照',
						icon: 'none'
					})
					return
				}
				if (!food_image) {
					uni.showToast({
						title: '请上传食品经营许可证',
						icon: 'none'
					})
					return
				}
				addSubmit({
					data: this.form,
					type: this.type
				}).then(res => {
					if (res.data.code == 1) {
						uni.showToast({
							title: '提交成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 2
							})
						}, 1000)

					}
				})
			}
		},
		onUnload() {

			uni.navigateBack({
				delta: 1
			})
		}
	}
</script>

<style lang="less">
	.u-nav-slot {
		position: relative;
		width: fit-content;
		font-size: 36rpx;
		color: #333333;
		font-weight: bold;

		.navline {
			width: 100%;
			height: 10rpx;
			background: #FE712A;
			opacity: 0.5;
			position: absolute;
			bottom: 0;
		}
	}

	.container {
		.form {
			border-top: #F4F6F8 solid 2rpx;
			padding: 6rpx 28rpx 0 31rpx;

			.formitem {
				height: 99rpx;
				display: flex;
				align-items: center;
				border-bottom: 1rpx solid #F4F6F8;
				padding: 0 2rpx 0 10rpx;
				font-size: 30rpx;
				color: #333333;

				.label {
					width: 211rpx;
					font-weight: bold;
				}

				.input {
					flex: 1;
				}

				.placeholder {
					font-size: 30rpx;
					color: #8B8B8B;
				}

				.more_icon {
					width: 21rpx;
					height: 31rpx;
				}
			}

			.form_title {
				height: 96rpx;
				display: flex;
				align-items: center;

				.line {
					width: 8rpx;
					height: 28rpx;
					background: #FE712A;
					border-radius: 4rpx;
					margin-right: 10rpx;
				}
			}
		}

	}

	.btn {
		width: 687rpx;
		line-height: 114rpx;
		background: linear-gradient(153deg, #FF8717, #FF5F00);
		border-radius: 57rpx;
		text-align: center;
		color: #FFFFFF;
		font-size: 36rpx;
		margin: 73rpx 32rpx;
	}

	.pic_box {
		display: flex;
		flex-wrap: wrap;

		.pic_item {
			width: 198rpx;
			height: 198rpx;
			position: relative;
			margin-right: 30rpx;
			margin-bottom: 20rpx;
			border-radius: 10rpx;
			overflow: hidden;
		}

		.pic_bg {
			width: 198rpx;
			height: 198rpx;
			position: absolute;
			border-radius: 10rpx;
			top: 0;
			left: 0;
		}

		.pic_item_name {
			text-align: center;
			width: 100%;
			position: absolute;
			bottom: 0;
			color: #fff;
			font-size: 26rpx;
			line-height: 60rpx;
		}
	}

	.delbox {
		width: 29rpx;
		height: 25rpx;
		background: rgba(0, 0, 0, .5);
		border-radius: 7rpx 0 0 12rpx;

		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		top: 0;
		right: 0;
	}

	.del_icon {
		width: 12rpx;
		height: 12rpx;
	}
</style>
